<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div class="title">简易教务管理系统</div>
      <div class="info">
        <span>{{identity==='student'?'学生':'教师'}}：{{name}}</span>
        <span> | </span>
        <span @click="loginOut" class="btn">退出</span>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <el-menu
          :default-active="activeIndex"
          router
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff">
          <el-menu-item :index="`/home/allcourse`">
            <i class="el-icon-menu"></i>
            <span slot="title">全部课程</span>
          </el-menu-item>
          <el-menu-item :index="`/home/course`">
            <i class="el-icon-menu"></i>
            <span slot="title">下学期课程</span>
          </el-menu-item>
          <el-menu-item :index="`/home/choose`" v-if="identity==='student'">
            <i class="el-icon-menu"></i>
            <span slot="title">选修课程</span>
          </el-menu-item>
          <el-menu-item :index="`/home/score`">
            <i class="el-icon-menu"></i>
            <span slot="title">本学期成绩</span>
          </el-menu-item>
          <el-menu-item :index="`/home/monitor`" v-if="identity==='teacher'">
            <i class="el-icon-menu"></i>
            <span slot="title">任命班长</span>
          </el-menu-item>
          <el-menu-item :index="`/home/password`">
            <i class="el-icon-menu"></i>
            <span slot="title">修改密码</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <!-- 主体内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { usernameAPI } from '@/api/userAPI'

export default {
  name: 'Home',
  data () {
    return {
      activeIndex: '',
      name: '',
      identity: ''
    }
  },
  created () {
    this.identity = JSON.parse(sessionStorage.getItem('token')).identity
    this.getUsername()
  },
  methods: {
    // 退出登录
    loginOut () {
      sessionStorage.removeItem('token')
      this.$message.success('已退出登录！')
      this.$router.push('/login')
    },
    // 获取用户姓名
    async getUsername () {
      const params = {
        username: JSON.parse(sessionStorage.getItem('token')).username,
        identity: this.identity
      }
      const { data: res } = await usernameAPI(params)
      if (res.status === 200) {
        this.name = res.data.username
      } else {
        this.$message.error(res.data.msg)
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .el-container {
    height: 100%;
    color: #fff;
  }

  .el-header{
    background-color: #373d41;
    text-align: center;
    line-height: 60px;
    display: flex;
    widows: 100%;
    justify-content: space-between;

    .title {
      font-weight: bold;
      font-size: 20px;

      &:hover {
        cursor: default
      }
    }

    .info {
      span {
        &:hover {
          cursor: default
        }
      }

      .btn {
        &:hover {
          cursor: pointer;
          color: skyblue
        }
      }
    }
  }

  .el-aside {
    background-color: #333744;
    text-align: left;
  }

  .el-main {
    background-color: #eaedf1;
  }
</style>
